//
//  MyBindingPhone2ViewController.swift
//  YLH
//
//  Created by qinjiangtao on 2024/1/12.
//

import UIKit

class MyBindingPhone2ViewController: QIBaseViewController {
    
    private var phoneTextF = UITextField()
    private var codeTextF = UITextField()
    private var getCodeBtn = GSCaptchaButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "绑定手机号"
        
        addUI()
    }
    

    
    private func addUI() {
        
        let titleL = UILabel().then({
            $0.frame = CGRect(x: 20, y: NAV_HEIGHT + 20, width: SCREEN_WIDTH - 40, height: 20)
            $0.text = "验证成功，请在下方输入新的手机号并填写验证码"
            $0.textColor = .black
            $0.font = SYSFONTBOLD(ofSize: 14)
        })
        view.addSubview(titleL)
        
        phoneTextF = UITextField().then {
            $0.frame = CGRect(x: 20, y: titleL.bottom + 20, width: 300, height: 40)
            $0.font = SYSFONT(ofSize: 16)
            $0.placeholder = "请输入手机号"
        }
        view.addSubview(phoneTextF)
        
        let lineView = UIView()
        lineView.frame = CGRect(x: 20, y: phoneTextF.bottom, width: SCREEN_WIDTH - 40, height: 1)
        lineView.backgroundColor = ColorEEEEEE
        view.addSubview(lineView)
        
        codeTextF = UITextField().then {
            $0.frame = CGRect(x: 20, y: lineView.bottom + 10, width: 200, height: 40)
            $0.font = SYSFONT(ofSize: 16)
            $0.placeholder = "请输入验证码"
        }
        view.addSubview(codeTextF)
        
        getCodeBtn = GSCaptchaButton().then {
            $0.frame = CGRect(x: SCREEN_WIDTH - 140, y: codeTextF.top, width: 120, height: codeTextF.height)
            $0.titleLabel?.font = SYSFONT(ofSize: 15)
            $0.setTitle("获取验证码", for: .normal)
            $0.setTitle("重新获取：second秒", for: .disabled)
            $0.setTitleColor(MainColor, for: .normal)
            $0.addTarget(self, action: #selector(getCodeBtnClick), for: .touchUpInside)
        }
        view.addSubview(getCodeBtn)
        
        let lineView1 = UIView()
        lineView1.frame = CGRect(x: 20, y: codeTextF.bottom, width: SCREEN_WIDTH - 40, height: 1)
        lineView1.backgroundColor = ColorEEEEEE
        view.addSubview(lineView1)
        
        let titleStr = """
        注：
        1.无法解绑手机号；手机号一旦换绑，则所关联的实名认证手机号、提现手机号将同步更换为新手机号。
        2.若您的帐号使用手机号登录，换绑后，将使用新号码作为登录手机号。
        """
        let title3L = YYLabel().then({
            $0.frame = CGRect(x: 24, y: lineView1.bottom + 20, width: SCREEN_WIDTH - 48, height: 120)
            $0.text = titleStr
            $0.numberOfLines = 0
            $0.textColor = Color999999
            $0.textVerticalAlignment = .top
            $0.font = SYSFONT(ofSize: 14)
        })
        view.addSubview(title3L)
        let att = NSMutableAttributedString(string: titleStr)
        att.yy_lineSpacing = 8
        att.yy_font = SYSFONT(ofSize: 14)
        att.yy_color = Color999999
        title3L.attributedText = att
        
        let submitBtn = UIButton().then {
            $0.frame = CGRect(x: 16, y: title3L.bottom + 40, width: SCREEN_WIDTH - 32, height: 44)
            $0.backgroundColor = MainColor
            $0.titleLabel?.font = SYSFONT(ofSize: 14)
            $0.setTitle("提交", for: .normal)
            $0.setTitleColor(.white, for: .normal)
            $0.addRoundedCorners(withRadius: 22)
            $0.addTarget(self, action: #selector(submitBtnClick), for: .touchUpInside)
        }
        view.addSubview(submitBtn)
        
    }
    
    @objc private func getCodeBtnClick() {
        let mobileStr = phoneTextF.text ?? ""
        guard mobileStr.count > 10 else {
            ShowHUD(str: "请输入正确手机号")
            return
        }
        
        let param = [
            "event": "changemobile",
            "mobile": mobileStr
        ]
        QINetworkManager.requestJson(urlStr: "/Sms/send", parameters: param) { [self] json in
            getCodeBtn.maxSecond = 60
            getCodeBtn.countdown = true
            ShowHUD(str: "验证码已发送")
        }
    }
    @objc private func submitBtnClick() {
        let mobileStr = phoneTextF.text ?? ""
        let codeStr = codeTextF.text ?? ""
        guard mobileStr.count > 10 else {
            ShowHUD(str: "请输入正确手机号")
            return
        }
        guard codeStr.count > 0 else {
            ShowHUD(str: "请输入验证码")
            return
        }
        let param = [
            "captcha": codeStr,
            "mobile": mobileStr
        ]
        QINetworkManager.requestJson(urlStr: "/User/bindMobile", parameters: param) { json in
            print(json)
            ShowHUD(str: "修改成功")
        }
    }

}
